package com.etc.myblog.ser;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.myblog.biz.ArticleBiz;
import com.etc.myblog.biz.ArticleCatBiz;
import com.etc.myblog.biz.CommentBiz;
import com.etc.myblog.biz.ReplyBiz;
import com.etc.myblog.biz.ScolumnBiz;
import com.etc.myblog.biz.UserBiz;
import com.etc.myblog.entity.Article;
import com.etc.myblog.entity.ArticleCat;
import com.etc.myblog.entity.ColumnArtCount;
import com.etc.myblog.entity.Comment;
import com.etc.myblog.entity.Reply;
import com.etc.myblog.entity.Scolumn;
import com.etc.myblog.entity.User;
import com.etc.myblog.entity.UserArtCount;

public class ArticleSer extends HttpServlet {

	ArticleCatBiz articleCatBiz = new ArticleCatBiz();
	ArticleBiz articleBiz = new ArticleBiz();
	ScolumnBiz scolumnBiz = new ScolumnBiz();
	UserBiz usBiz = new UserBiz();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//判断是否存在参数
		if (request.getParameter("tag")!=null) {   
			String tag = request.getParameter("tag");
			
			//根据参数值调用方法
			if (tag.equals("toaddpost")) {
				List<ArticleCat> list = new ArrayList<ArticleCat>();
				list = articleCatBiz.findAll();
				List<Scolumn> listScol = new ArrayList<Scolumn>();
				
				
				//获取当前登录用户
				User userLogin = new User();
				userLogin = (User) request.getSession().getAttribute("user");
				listScol = scolumnBiz.findByUserId(userLogin.getUserId());
				
				request.setAttribute("listScol", listScol);
				request.setAttribute("catList", list);
				request.getRequestDispatcher("user/postAdd.jsp").forward(request, response);
				return;
				
			} else if(tag.equals("addpost")) {
				//获取参数
				String articleTitle = request.getParameter("articleTitle");
				String articleContent = request.getParameter("articleContent");
				String articleRoundup = request.getParameter("articleRoundup");
				String articleType = request.getParameter("articleType");
				int catId = Integer.parseInt(request.getParameter("articleCat"));
				Article article = new Article();
				
				if (request.getParameter("columnId")!=null){
					Scolumn scolumn = new Scolumn();
					int scol = Integer.parseInt(request.getParameter("columnId"));
					scolumn = scolumnBiz.findById(scol);
					article.setColumn(scolumn);
				}

				//获取类别对象
				ArticleCat articleCat = new ArticleCat();
				articleCat = articleCatBiz.findById(catId);
				
				
				//设置对象属性
				
				article.setArticleType(articleType);
				article.setArticleStatus("未审核");
				article.setArticlecat(articleCat);
				article.setArticleContent(articleContent);
				article.setArticleTitle(articleTitle);
				article.setArticleRelTime(new Date());
				article.setArticleRoundup(articleRoundup);
				
				//判定是否存在文章id，存在的话表示编辑文章，不存在表示添加文章
				if (request.getParameter("articleId") == null || request.getParameter("articleId")=="" ) {
					
					//获取当前登录用户
					User userLogin = new User();
					userLogin = (User) request.getSession().getAttribute("user");
					article.setUser(userLogin);
					if(request.getParameter("columnId")==null || request.getParameter("columnId")=="") {
						articleBiz.addNoCol(article);
					} else {
						articleBiz.insertArticle(article);
					}
					
				} else {
					
					int articleId = Integer.parseInt(request.getParameter("articleId"));
					article.setArticleId(articleId);
					articleBiz.update(article);
				}
				
			}else if (tag.equals("detail")) {
				
				//获取文章id
				int articleId = Integer.parseInt(request.getParameter("articleid"));
				List<Article> aboutTenArt = new ArrayList<Article>();
				Article article = new Article();
				Article articleOne = new Article();
				List<Scolumn> userScolumn = new ArrayList<Scolumn>();
				//查询评论
				List<Comment> comms = new CommentBiz().findByartId(articleId);
				//回复
				Map<Integer,List<Reply>> replyMap = new HashMap<>();
				for (Comment comment : comms) {
					List<Reply> replies = new ReplyBiz().findRepByCommId(comment.getCommentId());
					replyMap.put(comment.getCommentId(), replies);
				}
				//根据文章id获取文章信息
				article = articleBiz.findById(articleId);
				//设置点击量+1
				articleBiz.setArtClick(articleId);
				//评论条数
				int Comcount = new CommentBiz().findCountByartId(articleId);
				//根据文章信息获取用户id，然后在此用户内获取点击量前10的文章
				aboutTenArt = articleBiz.findByUserIdTen(article.getUser().getUserId());
				//根据用户id获取专栏
				userScolumn = scolumnBiz.findByUserId(article.getUser().getUserId());
				//随机获取一个文章
				articleOne = articleBiz.findOne();
				
				//设置内置对象
				request.setAttribute("articleOne", articleOne);
				request.setAttribute("userScolumn", userScolumn);
				request.setAttribute("aboutTenArt", aboutTenArt);
				request.setAttribute("articleDetail", article);
				request.setAttribute("comments", comms);
				request.setAttribute("replyMap", replyMap);
				request.getRequestDispatcher("user/postDetail.jsp").forward(request, response);
				return;
				
			}else if (tag.equals("toedit")) {
				int articleId = Integer.parseInt(request.getParameter("articleid"));
				Article postedit = new Article();
				
				//根据文章id获取文章信息
				postedit = articleBiz.findById(articleId);
				
				
				//获取文章类别
				List<ArticleCat> list = new ArrayList();
				list = articleCatBiz.findAll();
				
				List<Scolumn> listScol = new ArrayList<Scolumn>();
				//获取当前登录用户
				User userLogin = new User();
				userLogin = (User) request.getSession().getAttribute("user");
				listScol = scolumnBiz.findByUserId(userLogin.getUserId());

				
				request.setAttribute("listScol", listScol);
				request.setAttribute("catList", list);
				request.setAttribute("postedit", postedit);
				//转发
				request.getRequestDispatcher("user/postAdd.jsp").forward(request, response);
				return;
				
			} else if (tag.equals("delete")) {
				int articleId = Integer.parseInt(request.getParameter("articleid"));
				articleBiz.delete(articleId);
			} else if (tag.equals("home")) {
				List<Article> artTop = new ArrayList<>();
				List<Scolumn> scolTop = new ArrayList<>();
				List<ColumnArtCount> caList = new ArrayList<>();
				
				//获取点击量前10的文章
				artTop = articleBiz.findClickTop();
				//随机获取10个专栏
				scolTop = scolumnBiz.findRandColTen();
				//设置专栏和专栏中的点击量
				for(int i=0; i<scolTop.size(); i++) {
					ColumnArtCount colArtCount = new ColumnArtCount();
					colArtCount.setScolumn(scolTop.get(i));
					colArtCount.setArticleView(scolumnBiz.articleView(scolTop.get(i).getColumnId()));
					caList.add(colArtCount);
				}
				
				request.setAttribute("caList", caList);
				request.setAttribute("artTop", artTop);
				request.getRequestDispatcher("user/webDefault.jsp").forward(request, response);
				return;
			} else if(tag.equals("catalog")) {
				if (request.getParameter("userId") != null || request.getParameter("userId")!="" ) {
					//获取当前登录用户
					int userId = Integer.parseInt(request.getParameter("userId"));
					User user = new User();
					
					user = usBiz.findById(userId);
					List<Article> list = new ArrayList<Article>();
					List<Article> aboutTenArt = new ArrayList<Article>();
					list = articleBiz.findByUserId(userId);
					List<Scolumn> userScolumn = new ArrayList<Scolumn>();
					
					//根据用户id获取专栏
					userScolumn = scolumnBiz.findByUserId(user.getUserId());
					//根据文章信息获取用户id，然后在此用户内获取点击量前10的文章
					aboutTenArt = articleBiz.findByUserIdTen(user.getUserId());
					
					//设置内置对象
					request.setAttribute("userScolumn", userScolumn);
					request.setAttribute("aboutTenArt", aboutTenArt);
					request.setAttribute("user", user);
					request.setAttribute("article", list);
					request.getRequestDispatcher("user/postCatalog.jsp").forward(request, response);
					return;
				
				}
			}
		}
		
		//判断是否登录，没有登录不允许进入文章中心
		if (request.getSession().getAttribute("user") == null) {
			 response.sendRedirect("user/userLogin.jsp");
			 return;
		}
		//如果是管理员跳转到管理中心
		User user = (User) request.getSession().getAttribute("user");
		if(user.getUserName().equals("root")) {
			System.out.println("111");
			request.getRequestDispatcher("AdArticleSer").forward(request, response);
			return;
		}
		User userLogin = new User();
		userLogin = (User) request.getSession().getAttribute("user");
		List<Article> list = new ArrayList<Article>();
		list = articleBiz.findByUserId(userLogin.getUserId());
		request.setAttribute("list", list);
		request.getRequestDispatcher("user/postList.jsp").forward(request, response);
	}

}
